package net.caixiaomi.info.ui.dialog;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.alibaba.fastjson.JSON;
import com.google.gson.reflect.TypeToken;
import com.sobot.chat.utils.ScreenUtils;

import net.caixiaomi.info.R;
import net.caixiaomi.info.adapter.DateAdapter;
import net.caixiaomi.info.app.CommonApp;
import net.caixiaomi.info.base.BaseDialogFragment;
import net.caixiaomi.info.base.quickadapter.BaseQuickAdapter;
import net.caixiaomi.info.model.CommonEvent;
import net.caixiaomi.info.model.CustomerModel;
import net.caixiaomi.info.util.EventWhat;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

import static android.R.id.button1;
import static android.R.id.list;

/**
 * 时间选择
 */

public class TimeDialogFragment extends BaseDialogFragment implements BaseQuickAdapter.OnItemClickListener {

    @BindView(list)
    RecyclerView mListView;

    @BindView(R.id.timer_group)
    RelativeLayout mTimerGroup;


    private DateAdapter mAdapter;
    private List<CustomerModel> mData = new ArrayList<>();

    @OnClick(button1)
    void toConfirm() {
        dismissAllowingStateLoss();
    }

    @Override
    protected int layoutRes() {
        return R.layout.dialog_time;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        String strData = getArguments().getString("data");
        List<CustomerModel> data = JSON.parseObject(strData, new TypeToken<List<CustomerModel>>() {}.getType());
        if (data != null && !data.isEmpty()) {
            mData.addAll(data);
        }
    }

    @Override
    protected void onViewCreated(@NonNull View view) {
        mListView.setLayoutManager(new LinearLayoutManager(getActivity()));
        mAdapter = new DateAdapter(R.layout.item_date);
        mAdapter.setOnItemClickListener(this);
        mAdapter.setNewData(mData);
        mListView.setAdapter(mAdapter);

        for(int i=0;i<mData.size();i++){
            if(mData.get(i).isSelected()){
                if(i+2<mData.size()){
                    mListView.smoothScrollToPosition(i+2);
                }else{
                    mListView.smoothScrollToPosition(mData.size()-1);
                }
            }
        }

        if(mData.size()>5){
            LinearLayout.LayoutParams params= (LinearLayout.LayoutParams) mTimerGroup.getLayoutParams();
                    params.height=2*CommonApp.mScreenHeight/3;
                    mTimerGroup.setLayoutParams(params);
        }

//        mTimerGroup.post(new Runnable() {
//            @Override
//            public void run() {
//                RelativeLayout.LayoutParams params= (RelativeLayout.LayoutParams) mListView.getLayoutParams();
//                int height=params.height;
//                if(params.bottomMargin> 2*CommonApp.mScreenHeight/3){
//                    params.height=2*CommonApp.mScreenHeight/3;
//                    mListView.setLayoutParams(params);
//                }
//            }
//        });


        getDialog().setCanceledOnTouchOutside(false);
        getDialog().setCancelable(false);
    }

    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
        try {
            Iterator<CustomerModel> iterator = mAdapter.getData().iterator();
            while (iterator.hasNext()) {
                CustomerModel next = iterator.next();
                next.setSelected(false);
            }
            mAdapter.getData().get(position).setSelected(true);
            mAdapter.notifyDataSetChanged();
            String text = JSON.toJSONString(mAdapter.getData());
            EventBus.getDefault().post(new CommonEvent(EventWhat.EVENT_TIME,text));
            dismissAllowingStateLoss();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
